using System;
using Castle.ActiveRecord;
using Castle.Components.Validator;
using NHibernate.Criterion;
using System.Collections.Generic;

namespace DecisionMaking.Domain
{
	[ActiveRecord]
	public class CueAttribute : ActiveRecordValidationBase<CueAttribute>
	{
		public CueAttribute ()
		{
		}
		
	    [PrimaryKey(PrimaryKeyType.Native)]
	    public int Id {set; get;}	
		
		[BelongsTo(NotNull = true, UniqueKey = "uix_cue_attribute_name")]
		public Environment Environment {set; get;}
		
		[Property(NotNull = true, UniqueKey = "uix_cue_attribute_name"), ValidateLength(2, 50)]
		public string Name {set; get;}		
			
		public static IList<CueAttribute> FindByEnvironmentId(int environmentId) 
		{
			return FindAll(
				DetachedCriteria.For(typeof(CueAttribute))
			    .CreateCriteria("Environment")
				.Add(Restrictions.IdEq(environmentId))	               
			);
			               
		}

        public static CueAttribute FindByEnvironmentAndName(int environmentId, string name)
        {
            return FindFirst(
                DetachedCriteria.For(typeof(CueAttribute))
                .Add(Restrictions.Eq("Name", name))
                .CreateCriteria("Environment")
                .Add(Restrictions.IdEq(environmentId))
            );
        }


	    public override bool Equals (object obj)
		{
			if (obj == null)
				return false;
			if (ReferenceEquals (this, obj))
				return true;
			if (obj.GetType () != typeof(CueAttribute))
				return false;
			DecisionMaking.Domain.CueAttribute other = (DecisionMaking.Domain.CueAttribute)obj;
			return Id == other.Id;
		}


		public override int GetHashCode ()
		{
			unchecked {
				return Id.GetHashCode ();
			}
		}

	}
}

